﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace DevScope.SharePoint.Utils.BulkDelete.Logging
{
    public enum LogEventTypeEnum
    {
        Debug = 100,
        Log = 200,
        Warning = 300,
        Error = 400,
    }

    public static class Logger
    {
        /// <summary>
        /// Errors the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Error(string message)
        {
            Error(null, message, null);
        }

        /// <summary>
        /// Errors the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="messageParameters">The message parameters.</param>
        public static void Error(string message, params object[] messageParameters)
        {
            Error(null, message, messageParameters);
        }

        /// <summary>
        /// Errors the specified ex.
        /// </summary>
        /// <param name="ex">The ex.</param>
        public static void Error(Exception ex)
        {
            Error(ex, string.Empty, null);
        }

        /// <summary>
        /// Errors the specified ex.
        /// </summary>
        /// <param name="ex">The ex.</param>
        /// <param name="message">The message.</param>
        public static void Error(Exception ex, string message)
        {
            Error(ex, message, null);
        }

        /// <summary>
        /// Errors the specified ex.
        /// </summary>
        /// <param name="ex">The ex.</param>
        /// <param name="message">The message.</param>
        /// <param name="messageParameters">The message parameters.</param>
        public static void Error(Exception ex, string message, params object[] messageParameters)
        {
            WriteLog(LogEventTypeEnum.Error, ex, message, messageParameters);
        }

        /// <summary>
        /// Logs the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Log(string message)
        {
            Log(message, null);
        }

        /// <summary>
        /// Logs the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="messageParameters">The message parameters.</param>
        public static void Log(string message, params object[] messageParameters)
        {
            WriteLog(LogEventTypeEnum.Log, null, message, messageParameters);
        }

        /// <summary>
        /// Warnings the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Warning(string message)
        {
            Warning(message, null);
        }

        /// <summary>
        /// Warnings the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="messageParameters">The message parameters.</param>
        public static void Warning(string message, params object[] messageParameters)
        {
            Warning(null, message, messageParameters);
        }

        /// <summary>
        /// Warnings the specified ex.
        /// </summary>
        /// <param name="ex">The ex.</param>
        /// <param name="message">The message.</param>
        /// <param name="messageParameters">The message parameters.</param>
        public static void Warning(Exception ex, string message, params object[] messageParameters)
        {
            WriteLog(LogEventTypeEnum.Warning, ex, message, messageParameters);
        }

        /// <summary>
        /// Debugs the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Debug(string message)
        {
            Debug(null, message, null);
        }

        /// <summary>
        /// Debugs the specified ex.
        /// </summary>
        /// <param name="ex">The ex.</param>
        /// <param name="message">The message.</param>
        public static void Debug(Exception ex, string message)
        {
            Debug(ex, message, null);
        }

        /// <summary>
        /// Debugs the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="messageParameters">The message parameters.</param>
        public static void Debug(string message, params object[] messageParameters)
        {
            Debug(null, message, messageParameters);
        }

        /// <summary>
        /// Debugs the specified ex.
        /// </summary>
        /// <param name="ex">The ex.</param>
        /// <param name="message">The message.</param>
        /// <param name="messageParameters">The message parameters.</param>
        public static void Debug(Exception ex, string message, params object[] messageParameters)
        {
            WriteLog(LogEventTypeEnum.Debug, ex, message, messageParameters);
        }

        /// <summary>
        /// Writes the log.
        /// </summary>
        /// <param name="evtType">Type of the evt.</param>
        /// <param name="ex">The ex.</param>
        /// <param name="message">The message.</param>
        /// <param name="messageParameters">The message parameters.</param>
        private static void WriteLog(LogEventTypeEnum evtType, Exception ex, string message, params object[] messageParameters)
        {
            try
            {
                message = string.Format("{0} - ({1:yyyy-MM-dd HH:mm:ss}): {2}\r\n"
                 , evtType
                 , DateTime.Now
                 , FormatMessage(ex, message, messageParameters));

                System.Diagnostics.Trace.WriteLine(message);
                
                //Debug's nao vao pra consola
                if (evtType != LogEventTypeEnum.Debug)
                    Console.WriteLine(message);
            }
            catch (Exception lex)
            {
                System.Diagnostics.Trace.WriteLine("Error logging. Exception: '{0}'", lex.ToString());
            }
        }

        /// <summary>
        /// Formats the message.
        /// </summary>
        /// <param name="ex">The ex.</param>
        /// <param name="message">The message.</param>
        /// <param name="messageParameters">The message parameters.</param>
        /// <returns></returns>
        public static string FormatMessage(Exception ex, string message, object[] messageParameters)
        {
            if (string.IsNullOrEmpty(message))
                message = string.Empty;
            else
                if (messageParameters != null)
                    message = string.Format(message, messageParameters);

            if (ex != null)
                message = string.Format("{0}{1}{2}", message, System.Environment.NewLine, ex.ToString());

            return message;
        }

    }
}
